package com.seeyon.apps.ddkk.manager.datasync;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.kk.client.PTRWebServiceStub;
import com.seeyon.apps.kk.client.PTRWebServiceStub.GetActivityInfo;
import com.seeyon.apps.kk.client.PTRWebServiceStub.GetActivityInfoResponse;
import com.seeyon.apps.kk.client.PTRWebServiceStub.GetAttendeeInfo;
import com.seeyon.apps.kk.client.PTRWebServiceStub.GetAttendeeInfoResponse;
import com.seeyon.apps.kk.client.PTRWebServiceStub.GetHCPCostInfo;
import com.seeyon.apps.kk.client.PTRWebServiceStub.GetHCPCostInfoResponse;
import com.seeyon.apps.kk.manager.FreemarkFlowManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.v3x.services.form.FormFactory;
import com.seeyon.v3x.services.form.FormUtils;
import com.seeyon.v3x.services.form.bean.FormExport;


public class DefaultAbstractDataSyncManager extends AbstractDataSyncManager {
	private static final Log log = LogFactory.getLog(DefaultAbstractDataSyncManager.class);
	private String sourceJdbcTemplateBeanId;
	private String sourceFindSqlFtl;
	private String sourceInsertSqlFtl;
	private FreemarkFlowManager freemarkFlowManager;
	private String addFindSqlFtl;
	private String addCreateXMLFtl;
	private String updateFindSqlFtl;
	private String updateSqlFtl;
	private String adminLoginName;
	private String templateCode;
	private String beforeGrabDataSqlFtl;
	private String addCreateDbXMLFtl;

	public String getAddCreateDbXMLFtl() {
		return addCreateDbXMLFtl;
	}

	public void setAddCreateDbXMLFtl(String addCreateDbXMLFtl) {
		this.addCreateDbXMLFtl = addCreateDbXMLFtl;
	}

	public String getAdminLoginName() {
		return adminLoginName;
	}

	public void setAdminLoginName(String adminLoginName) {
		this.adminLoginName = adminLoginName;
	}

	public String getTemplateCode() {
		return templateCode;
	}

	public void setTemplateCode(String templateCode) {
		this.templateCode = templateCode;
	}

	public String getBeforeGrabDataSqlFtl() {
		return beforeGrabDataSqlFtl;
	}

	public void setBeforeGrabDataSqlFtl(String beforeGrabDataSqlFtl) {
		this.beforeGrabDataSqlFtl = beforeGrabDataSqlFtl;
	}

	public String getSourceJdbcTemplateBeanId() {
		return sourceJdbcTemplateBeanId;
	}

	public void setSourceJdbcTemplateBeanId(String sourceJdbcTemplateBeanId) {
		this.sourceJdbcTemplateBeanId = sourceJdbcTemplateBeanId;
	}

	public String getSourceFindSqlFtl() {
		return sourceFindSqlFtl;
	}

	public void setSourceFindSqlFtl(String sourceFindSqlFtl) {
		this.sourceFindSqlFtl = sourceFindSqlFtl;
	}

	public String getSourceInsertSqlFtl() {
		return sourceInsertSqlFtl;
	}

	public void setSourceInsertSqlFtl(String sourceInsertSqlFtl) {
		this.sourceInsertSqlFtl = sourceInsertSqlFtl;
	}

	public FreemarkFlowManager getFreemarkFlowManager() {
		return freemarkFlowManager;
	}

	public void setFreemarkFlowManager(FreemarkFlowManager freemarkFlowManager) {
		this.freemarkFlowManager = freemarkFlowManager;
	}

	public String getAddFindSqlFtl() {
		return addFindSqlFtl;
	}

	public void setAddFindSqlFtl(String addFindSqlFtl) {
		this.addFindSqlFtl = addFindSqlFtl;
	}

	public String getAddCreateXMLFtl() {
		return addCreateXMLFtl;
	}

	public void setAddCreateXMLFtl(String addCreateXMLFtl) {
		this.addCreateXMLFtl = addCreateXMLFtl;
	}

	public String getUpdateFindSqlFtl() {
		return updateFindSqlFtl;
	}

	public void setUpdateFindSqlFtl(String updateFindSqlFtl) {
		this.updateFindSqlFtl = updateFindSqlFtl;
	}

	public String getUpdateSqlFtl() {
		return updateSqlFtl;
	}

	public void setUpdateSqlFtl(String updateSqlFtl) {
		this.updateSqlFtl = updateSqlFtl;
	}

	public boolean beforeGrabData() {
		if (StringUtils.isNotEmpty(this.beforeGrabDataSqlFtl)) {
			String sql = this.freemarkFlowManager.fltTemplateIntoString(this.beforeGrabDataSqlFtl, null);
			this.jdbcTemplate.update(sql);
		}
		return false;
	}
//数据映射
	@Override
	//1900-01-02 00:00:00.0
	public boolean grabData() {
		if (StringUtils.isNotEmpty(this.sourceFindSqlFtl)) {
			String sql = this.freemarkFlowManager.fltTemplateIntoString(sourceFindSqlFtl, null);
			List<Map<String, Object>> vl = this.jdbcTemplate.queryForList(sql);			
			if (!vl.isEmpty()) {
				String[] batchinsertSql = new String[vl.size()];
				int i = 0;				
				for (Iterator<Map<String, Object>> iterator = vl.iterator(); iterator.hasNext();) {
					Map<String, Object> map = iterator.next();
					batchinsertSql[i] = this.freemarkFlowManager.fltTemplateIntoString(sourceInsertSqlFtl, map);
					i++;
				}
				//增量
				this.jdbcTemplate.batchUpdate(batchinsertSql);			
			}
		}
		return false;
	}
//增量比对
	@Override
	public boolean addData() {
	if (StringUtils.isNotEmpty(this.addFindSqlFtl)) {
			String addFindSql = this.freemarkFlowManager.fltTemplateIntoString(addFindSqlFtl, null);
			List<Map<String, Object>> vl = this.jdbcTemplate.queryForList(addFindSql);
			for (Iterator<Map<String, Object>> iterator = vl.iterator(); iterator.hasNext();) {
				Map<String, Object> map = iterator.next();			
				String dataXml = this.freemarkFlowManager.fltTemplateIntoString(addCreateDbXMLFtl, map);
				log.info("===========================");

				log.info(dataXml);
				log.info("===========================");

				FormFactory formFactory = (FormFactory) AppContext.getBean("formFactory");
				try {
					FormExport[] formExportData = FormUtils.xmlTransformFormExports(dataXml);
					for (FormExport formExport : formExportData) {
						formFactory.importBusinessFormData(this.adminLoginName, this.templateCode, formExport, null);
					}
				} catch (Exception e) {
					log.error(e);
				}
			}
		}
		return false;
	}
//变更比对
	@Override
	public boolean updateData() {
		if (StringUtils.isNotEmpty(this.updateFindSqlFtl)) {
			String updateFindSql = this.freemarkFlowManager.fltTemplateIntoString(updateFindSqlFtl, null);
			List<Map<String, Object>> vl = this.jdbcTemplate.queryForList(updateFindSql);
			if (!vl.isEmpty()) {
				String[] batchSql = new String[vl.size()];
				int i = 0;
				for (Iterator<Map<String, Object>> iterator = vl.iterator(); iterator.hasNext();) {
					Map<String, Object> map = iterator.next();
					batchSql[i] = this.freemarkFlowManager.fltTemplateIntoString(updateSqlFtl, map);
					i++;
				}
				this.jdbcTemplate.batchUpdate(batchSql);
			}
		}
		return false;
	}

	@Override
	public boolean deleteData() {
		return false;
	}

}
